/*     */ package cn.zzuisa.news.servlet;
/*     */ 
/*     */ import cn.zzuisa.news.DTO.CommentDTO;
/*     */ import cn.zzuisa.news.DTO.NewsDTO;
/*     */ import cn.zzuisa.news.convertor.Comment2CommentDTO;
/*     */ import cn.zzuisa.news.convertor.News2NewsDTO;
/*     */ import cn.zzuisa.news.entity.Comment;
/*     */ import cn.zzuisa.news.entity.News;
/*     */ import cn.zzuisa.news.entity.Page;
/*     */ import cn.zzuisa.news.entity.User;
/*     */ import cn.zzuisa.news.enums.CommonEnums;
/*     */ import cn.zzuisa.news.form.NewsEditForm;
/*     */ import cn.zzuisa.news.form.NewsForm;
/*     */ import cn.zzuisa.news.service.CommentService;
/*     */ import cn.zzuisa.news.service.NewsService;
/*     */ import cn.zzuisa.news.service.NewsTypeService;
/*     */ import cn.zzuisa.news.service.impl.CommentServiceImpl;
/*     */ import cn.zzuisa.news.service.impl.NewsServiceImpl;
/*     */ import cn.zzuisa.news.service.impl.NewsTypeServiceImpl;
/*     */ import cn.zzuisa.news.util.JsonUtil;
/*     */ import cn.zzuisa.news.util.ResultVOUtil;
/*     */ import com.google.gson.Gson;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.annotation.WebServlet;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ @WebServlet(name = "news", urlPatterns = {"/news"}, asyncSupported = true)
/*     */ public class NewsServlet
/*     */   extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*  37 */   private NewsService newsServiceImpl = new NewsServiceImpl();
/*  38 */   private NewsTypeService newsTypeServiceImpl = new NewsTypeServiceImpl();
/*  39 */   private CommentService commentServiceImpl = new CommentServiceImpl();
/*     */ 
/*     */ 
/*     */   
/*     */   protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*  44 */     String method = req.getParameter("method");
/*  45 */     if (method.equals("add")) {
/*  46 */       String json = JsonUtil.getStrResponse(req);
/*  47 */       NewsForm newsForm = (NewsForm)(new Gson()).fromJson(json, NewsForm.class);
/*  48 */       if (this.newsServiceImpl.add(newsForm)) {
/*  49 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
/*     */       } else {
/*  51 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
/*     */       } 
/*  53 */     } else if (method.equals("add_page")) {
/*  54 */       List<String> showTypes = this.newsTypeServiceImpl.showTypes();
/*  55 */       List<String> showTypesId = this.newsTypeServiceImpl.showTypesId();
/*  56 */       req.setAttribute("types", showTypes);
/*  57 */       req.setAttribute("typesid", showTypesId);
/*  58 */       req.getRequestDispatcher("WEB-INF/ftl/index/addNews.ftl").forward(req, resp);
/*  59 */     } else if (method.equals("addType_page")) {
/*  60 */       req.getRequestDispatcher("WEB-INF/ftl/news/addType_page.ftl").forward(req, resp);
/*  61 */     } else if (method.equals("addType")) {
/*  62 */       String type = req.getParameter("type");
/*  63 */       System.out.println("tttt:" + type);
/*  64 */       if (this.newsTypeServiceImpl.add(type)) {
/*  65 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
/*     */       } else {
/*  67 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
/*     */       }
/*     */     
/*  70 */     } else if (method.equals("edit_page")) {
/*  71 */       String id = req.getParameter("id");
/*  72 */       id = (id == null) ? "1" : id;
/*  73 */       News news = this.newsServiceImpl.findById(Integer.valueOf(Integer.parseInt(id)));
/*  74 */       req.setAttribute("news", news);
/*  75 */       req.getRequestDispatcher("WEB-INF/ftl/news/editNews.ftl").forward(req, resp);
/*  76 */     } else if (method.equals("edit")) {
/*  77 */       String json = JsonUtil.getStrResponse(req);
/*  78 */       NewsEditForm newsEditForm = (NewsEditForm)(new Gson()).fromJson(json, NewsEditForm.class);
/*  79 */       if (this.newsServiceImpl.edit(newsEditForm))
/*  80 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success())); }
/*     */       else
/*  82 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR))); } 
/*  83 */     } else if (method.equals("list")) {
/*     */       Integer size, current;
/*     */ 
/*     */       
/*  87 */       String type = req.getParameter("type");
/*     */ 
/*     */       
/*  90 */       Page page = null;
/*  91 */       List<String> types = null;
/*  92 */       List<NewsDTO> newsDTOs = null;
/*     */       try {
/*  94 */         current = Integer.valueOf(Integer.parseInt(req.getParameter("page")));
/*  95 */         size = Integer.valueOf(Integer.parseInt(req.getParameter("size")));
/*  96 */       } catch (NumberFormatException e) {
/*  97 */         current = Integer.valueOf(1);
/*  98 */         size = Integer.valueOf(10);
/*     */       } 
/*     */       
/* 101 */       User user = (User)req.getSession().getAttribute("user");
/* 102 */       String sql = "news_state = 1 and";
/* 103 */       System.out.println("role：" + user.getRole());
/* 104 */       if (type == null) {
/* 105 */         page = (user.getRole().intValue() == 1) ? this.newsServiceImpl.page(current, size, null) : 
/* 106 */           this.newsServiceImpl.page(current, size, sql);
/* 107 */         newsDTOs = News2NewsDTO.convert(page.getData());
/*     */       } else {
/* 109 */         type = new String(type.getBytes("ISO-8859-1"), "UTF-8");
/* 110 */         page = (user.getRole().intValue() == 1) ? this.newsServiceImpl.ListByTypesName(current, size, null, type) : 
/* 111 */           this.newsServiceImpl.ListByTypesName(current, size, sql, type);
/* 112 */         newsDTOs = News2NewsDTO.convert(page.getData());
/* 113 */         req.setAttribute("newsType", type);
/*     */       } 
/* 115 */       page.setPageNo(current.intValue());
/* 116 */       types = this.newsTypeServiceImpl.showTypes();
/* 117 */       page.setData(newsDTOs);
/* 118 */       req.setAttribute("lists", page);
/* 119 */       req.setAttribute("types", types);
/* 120 */       req.getRequestDispatcher("WEB-INF/ftl/news/list.ftl").forward(req, resp);
/* 121 */     } else if (method.equals("detail_page")) {
/* 122 */       Integer id = Integer.valueOf(Integer.parseInt(req.getParameter("id")));
/* 123 */       News news = this.newsServiceImpl.findById(id);
/* 124 */       List<Comment> comments = this.commentServiceImpl.list(id);
/* 125 */       if (req.getHeader("x-requested-with") == null || 
/* 126 */         !req.getHeader("x-requested-with").equals("XMLHttpRequest")) {
/* 127 */         this.newsServiceImpl.click(id);
/*     */       }
/* 129 */       List<CommentDTO> commentDTOs = Comment2CommentDTO.convert(comments);
/* 130 */       NewsDTO newsDTO = News2NewsDTO.convert(news);
/*     */       
/* 132 */       req.setAttribute("commentDTOs", commentDTOs);
/* 133 */       req.setAttribute("news", newsDTO);
/* 134 */       req.getRequestDispatcher("WEB-INF/ftl/index/detail.ftl").forward(req, resp);
/* 135 */     } else if (method.equals("remove")) {
/* 136 */       Integer id = Integer.valueOf(Integer.parseInt(req.getParameter("id")));
/* 137 */       if (this.newsServiceImpl.setState(Integer.valueOf(2), id))
/* 138 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success())); }
/*     */       else
/* 140 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR))); } 
/* 141 */     } else if (method.equals("pass")) {
/* 142 */       Integer id = Integer.valueOf(Integer.parseInt(req.getParameter("id")));
/* 143 */       if (this.newsServiceImpl.setState(Integer.valueOf(1), id))
/* 144 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success())); }
/*     */       else
/* 146 */       { resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR))); } 
/* 147 */     } else if (method.equals("search")) {
/* 148 */       Integer size, current; resp.setCharacterEncoding("UTF-8");
/* 149 */       resp.setHeader("content-type", "text/html;charset=UTF-8");
/* 150 */       String key = req.getParameter("key");
/* 151 */       System.out.println("key=" + key);
/* 152 */       key = (key == null) ? "" : key;
/*     */       
/* 154 */       System.out.println("key2=" + key);
/*     */ 
/*     */       
/* 157 */       Page page = null;
/* 158 */       List<NewsDTO> newsDTOs = null;
/*     */       try {
/* 160 */         current = Integer.valueOf(Integer.parseInt(req.getParameter("page")));
/* 161 */         size = Integer.valueOf(Integer.parseInt(req.getParameter("size")));
/* 162 */       } catch (NumberFormatException e) {
/* 163 */         current = Integer.valueOf(1);
/* 164 */         size = Integer.valueOf(10);
/*     */       } 
/* 166 */       User user = (User)req.getSession().getAttribute("user");
/* 167 */       String sql = "news_state = 1 and";
/* 168 */       System.out.println("role：" + user.getRole());
/* 169 */       page = (user.getRole().intValue() == 1) ? this.newsServiceImpl.search(current, size, null, key) : 
/* 170 */         this.newsServiceImpl.search(current, size, sql, key);
/* 171 */       newsDTOs = News2NewsDTO.convert(page.getData());
/* 172 */       page.setPageNo(current.intValue());
/* 173 */       page.setData(newsDTOs);
/* 174 */       req.setAttribute("lists", page);
/* 175 */       req.getRequestDispatcher("WEB-INF/ftl/news/list.ftl").forward(req, resp);
/* 176 */     } else if (method.equals("sort")) {
/* 177 */       Integer size, current; String key = req.getParameter("key");
/* 178 */       String type = req.getParameter("type");
/*     */ 
/*     */       
/* 181 */       Page page = null;
/* 182 */       List<String> types = null;
/* 183 */       List<NewsDTO> newsDTOs = null;
/*     */       try {
/* 185 */         current = Integer.valueOf(Integer.parseInt(req.getParameter("page")));
/* 186 */         size = Integer.valueOf(Integer.parseInt(req.getParameter("size")));
/* 187 */       } catch (NumberFormatException e) {
/* 188 */         current = Integer.valueOf(1);
/* 189 */         size = Integer.valueOf(10);
/*     */       } 
/* 191 */       User user = (User)req.getSession().getAttribute("user");
/* 192 */       String sql = "news_state = 1 and";
/* 193 */       System.out.println("role：" + user.getRole());
/* 194 */       if (type == null) {
/* 195 */         page = (user.getRole().intValue() == 1) ? this.newsServiceImpl.pagelist(current, size, null, key) : 
/* 196 */           this.newsServiceImpl.pagelist(current, size, sql, key);
/* 197 */         newsDTOs = News2NewsDTO.convert(page.getData());
/*     */       } else {
/* 199 */         type = new String(type.getBytes("ISO-8859-1"), "UTF-8");
/* 200 */         page = (user.getRole().intValue() == 1) ? this.newsServiceImpl.pagelist(current, size, null, key) : 
/* 201 */           this.newsServiceImpl.pagelist(current, size, sql, key);
/* 202 */         newsDTOs = News2NewsDTO.convert(page.getData());
/*     */       } 
/*     */       
/* 205 */       page.setPageNo(current.intValue());
/* 206 */       types = this.newsTypeServiceImpl.showTypes();
/* 207 */       page.setData(newsDTOs);
/* 208 */       req.setAttribute("lists", page);
/* 209 */       req.setAttribute("types", types);
/* 210 */       req.setAttribute("type", key);
/* 211 */       req.getRequestDispatcher("WEB-INF/ftl/news/list.ftl").forward(req, resp);
/*     */     } 
/*     */   }
/*     */ }


/* Location:              /Users/frank/Desktop/admin/WEB-INF/classes/!/cn/zzuisa/news/servlet/NewsServlet.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */